package com.h2cloud.ThreadLocalTest.customExecutor;

import cn.hutool.core.lang.func.VoidFunc0;
import cn.hutool.cron.task.RunnableTask;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;
import com.alibaba.ttl.threadpool.TtlExecutors;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Package: com.h2cloud.ThreadLocalTest.customExecutor
 * @ClassName: Main
 * @Author: h2cloud
 * @CreateTime: 2023/12/21 11:09 下午
 * @Description:
 */
// 自定义线程池，包装 Runnable方法，传递线程上下文
public class Main {
	public static void main(String[] args) {
		CurrentUser.UserInfo userInfo = new CurrentUser.UserInfo();
		userInfo.setUserId(1L);
		userInfo.setMobile("1");
		userInfo.setName("longkai");
		CurrentUser.set(userInfo);

		AtomicInteger threadNumber = new AtomicInteger(0);
		ExecutorService asyncExecutorService = new TransmittableThreadPoolExecutor(
				2,
				2,
				60,
				TimeUnit.SECONDS,
				new ArrayBlockingQueue<>(16),
				new ThreadFactory() {
					@Override
					public Thread newThread(Runnable r) {
						return new Thread(r, "async-exec-" + threadNumber.getAndIncrement());
					}
				},
				new RejectedExecutionHandler() {
					@Override
					public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
						if (!executor.isShutdown()) {
							try {
								//尝试阻塞式加入任务队列
								executor.getQueue().put(r);
							} catch (InterruptedException e) {
								//保持线程的中端状态
								Thread.currentThread().interrupt();
							}
						}
					}
				});

		asyncExecutorService.submit(() -> doSomething());
		asyncExecutorService.submit(() -> doSomething());
		asyncExecutorService.submit(() -> doSomething());
		// try {
		// 	Thread.sleep(2300);
		// } catch (InterruptedException e) {
		// 	e.printStackTrace();
		// }
		// userInfo.setName("hhhhh");
		// asyncExecutorService.submit(() -> doSomething());
		// asyncExecutorService.submit(() -> doSomething());
		// asyncExecutorService.submit(() -> doSomething());

		fun2();
		ExecutorService executorService = Executors.newFixedThreadPool(1);
		executorService.submit(() -> doSomething());

	}

	public static void doSomething() {
		try {
			Thread.sleep(600);
			if (CurrentUser.get() != null) {
				System.out.println(Thread.currentThread().getName() + "  " + CurrentUser.get().getName() + "  ");
			} else {
				System.out.println("is null");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void fun1() {
		CurrentUserV2.UserInfo userInfo = new CurrentUserV2.UserInfo();
		userInfo.setUserId(1L);
		userInfo.setMobile("1");
		userInfo.setName("longkai");
		CurrentUserV2.set(userInfo);

	}

	public static void fun2() {
		ExecutorService asyncExecutorService = Executors.newFixedThreadPool(2);
		Runnable task = () -> {
			System.out.println("-------------asyncExecutorService");
		};
		asyncExecutorService.execute(task);
		asyncExecutorService.execute(task);


		TransmittableThreadLocal<String> context2 = new TransmittableThreadLocal<>();
		context2.set("fun2");
		ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(asyncExecutorService);
		Runnable task2 = () -> {
			System.out.println("-------------" + context2.get());
		};
		ttlExecutorService.submit(task2);
	}
}
